package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-08
 */
@Service  //（一般）EduSubjectServiceImpl把这个对象创建，交给spring管理
//EduSubjectServiceImpl不能交给spring管理，因为这个是new的方法
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        //EasyExcel这个有一个异常
        //捕获异常
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            //方法中，第一个写流,第二个写实体类,再new一个监听器
            //sheet,表格左下角的，分列，  do.read只读
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //课程分类列表（树形）
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1查询所有一级分类 parentid = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0"); //设置值不等于0
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne); //baseMapper已经自动注入，不需要，再次，注入
        //this.list(wrapperOne) 也可以调用 当前自己对象

        //2查询所有二级分类 parentid !=0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0"); //设置值等于0
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo); //baseMapper已经自动注入，不需要，再次，注入

        //创建list集合，用于存储最终，封装的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //3封装一级分类
        //查询出来所有一级分类list集合遍历，得到每一个分类对象，获取每个一级分类的对象值
         //循环遍历出来是，树状图
        //封装到要求的list集合里面List<OneSubject> finalSubjectList
        for (int i = 0; i < oneSubjectList.size(); i++) {//遍历oneSubjectList集合
            //得到oneSubjectList每个edeSubject对象
            EduSubject eduSubject = oneSubjectList.get(i);

            //把eduSubject里面值获取出来，放到OneSubject对象里面

            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //把edu中的值，先get出来，再set到one里面
            //edeSubject值复制到对应oneSubject对象里面
            BeanUtils.copyProperties(eduSubject, oneSubject);
            //多个OneSubject放到finalSubjectList里面
            finalSubjectList.add(oneSubject);

            //在一级分类循环遍历查询所有的二级分类
            //创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            //遍历二级分类的list结合
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectList.get(m);
                //查询是所有二级分类，能不能直接封装，要看二级分类，是哪个一级分类（判断）
                //判断二级分类parentid和一级分类的id是否一样
                if (tSubject.getParentId().equals(eduSubject.getId())) {
                    //把tSubject值复制到TwoSubject里面，放到twoFinalSubjectList里面
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            //把一级下面所有二级分类放到一级分类里面
            //twoFinalSubjectList所有二级分类  setChildren里面有集合二级分类
            oneSubject.setChildren(twoFinalSubjectList);
        }
        return finalSubjectList;
    }
}